import { Node } from '@antv/x6'
import { canvas } from './canvasInstance'
import { storeToRefs } from 'pinia'
import { useSimControllerStore } from '../message/simController'

//生成画布动画
export const startcCanvasSim = (data: any) => {
  const { type, state, parameter } = data
  // console.log(type, state, parameter)
  if (type === 'product') {
    const { targetId, productId } = parameter
    const targetNode = canvas.getCellById(targetId)
    let nodeX = 0
    let nodeY = 0
    let width = 0
    let height = 0

    if (canvas.isNode(targetNode)) {
      const { x, y } = targetNode.getPosition()
      width = targetNode.getSize().width
      height = targetNode.getSize().height
      nodeX = x + width / 2
      nodeY = y + height / 2
    }

    if (state === 'generate') {
      //获取产品的样式
      const id = parameter.targetId
      const nodes = window.api.handleCanvasData('getNodes', 'canvas2D', false, ['id', id])
      const node = nodes[0]
      const productStyle = node.simData.product
      canvas.addNode({
        shape: 'product-node',
        id: productId,
        x: nodeX,
        y: nodeY,
        width: productStyle?.width || 20,
        height: productStyle?.height || 20,
        data: {
          style: { ...productStyle }
        }
      })
    } else if (state === 'move') {
      const node = canvas.getCellById(productId) as Node
      const { x: sourceX, y: sourceY } = node.getPosition()
      const dx = nodeX - sourceX
      const dy = nodeY - sourceY

      node.translate(dx, dy, {
        transition: {
          duration: parameter.duration
        }
      })
      if (targetNode.zIndex) {
        node.setZIndex(targetNode.zIndex)
      }
      // node.position(nodeX, nodeY)
    } else if (state === 'transport') {
      const node = canvas.getCellById(productId) as Node
      const { x, y, duration, angle } = parameter
      const { x: sourceX, y: sourceY } = node.getPosition()
      const dx = x - sourceX
      const dy = y - sourceY
      if (duration === 0) {
        node.position(x, y)
      } else {
        node.translate(dx, dy, {
          transition: {
            duration: duration * 1000
          }
        })
      }
      if (targetNode.zIndex) {
        node.setZIndex(targetNode.zIndex)
      }
      node.setData({
        angle: angle
      })
    } else if (state === 'recycle') {
      const node = canvas.getCellById(productId)
      setTimeout(() => {
        canvas.removeCell(node)
      }, 300)
    } else if (state === 'startProcessing') {
      const node = canvas.getCellById(productId)
      node.setData({
        status: 'processing'
      })
    } else if (state === 'finishProcessing') {
      const node = canvas.getCellById(productId)
      node.setData({
        status: 'finish'
      })
    }
  } else if (type === 'simTime') {
    const { simTime } = storeToRefs(useSimControllerStore())
    simTime.value = parameter
  } else if (type === 'agv') {
    if (state === 'transport') {
      const { targetId } = parameter
      const { x, y, angle } = parameter
      const targetNode = canvas.getCellById(targetId)

      if (canvas.isNode(targetNode)) {
        const { width, height } = targetNode.getSize()
        targetNode.position(x - width / 2, y - height / 2)
        targetNode.setData({
          angle: angle
        })
      }
    }
  } else if (type === 'style') {
    const { targetId, style } = parameter
    const targetNode = canvas.getCellById(targetId)
    const oldData = targetNode.getData()
    targetNode.setData({
      ...oldData,
      ...style
    })
  } else if (type === 'workerPool') {
    if (state === 'generate') {
      const { targetId, workerId } = parameter

      const targetNode = canvas.getCellById(targetId) as Node
      const { x, y } = targetNode.getPosition()
      canvas.addNode({
        shape: 'transport-node',
        id: workerId,
        x: x,
        y: y,
        data: {
          icon: 'worker',
          borderWidth: 0,
          iconSize: 3,
          name: ''
        }
      })
    } else if (state === 'transport') {
      const { targetId, x, y, angle } = parameter
      const node = canvas.getCellById(targetId) as Node
      node.position(x, y)
      node.setData({
        angle: angle
      })
    } else if (state === 'recycle') {
      const { targetId } = parameter
      const node = canvas.getCellById(targetId)
      canvas.removeCell(node)
    }
  }
}
